﻿using System;
using System.Data;
using System.Data.SqlClient;
namespace MDA
{

/// <summary>
/// 表T_LOG_DETAILS数据访问类
/// </summary>
public class T_LOG_DETAILSAccess 
{
	public SqlConnection connection = new SqlConnection(ConnectData.ConnectString);
	/// <summary>
	/// 获取将表数据插入数据库的命令对象(SqlCommand).将插入所有字段。
	/// </summary>
	/// <returns>返回一个插入命令对象(SqlCommand)</returns>
	public SqlCommand GetInsertCommand(){
		SqlCommand insert = new SqlCommand("insert into [T_LOG_DETAILS]([OPERATETIME],[LOGID],[OPERATEBUTTON],[REMARK],[MENUID])values(@OPERATETIME,@LOGID,@OPERATEBUTTON,@REMARK,@MENUID)",connection);
		insert.Parameters.Add("@OPERATETIME",SqlDbType.DateTime);
		insert.Parameters["@OPERATETIME"].SourceColumn="OPERATETIME";
		insert.Parameters.Add("@LOGID",SqlDbType.Int);
		insert.Parameters["@LOGID"].SourceColumn="LOGID";
		insert.Parameters.Add("@OPERATEBUTTON",SqlDbType.VarChar);
		insert.Parameters["@OPERATEBUTTON"].SourceColumn="OPERATEBUTTON";
		insert.Parameters.Add("@REMARK",SqlDbType.VarChar);
		insert.Parameters["@REMARK"].SourceColumn="REMARK";
		insert.Parameters.Add("@MENUID",SqlDbType.Int);
		insert.Parameters["@MENUID"].SourceColumn="MENUID";
		return insert;
	}
	/// <summary>
	/// 获取一个更新数据表的命令对象。这个方法将更新主键字段和其它所有字段。
	/// </summary>
	/// <returns>返回一个更新命令对象(SqlCommand)</returns>
	public SqlCommand GetUpdateCommand_P(){
		SqlCommand update = new SqlCommand("update [T_LOG_DETAILS] set [OPERATETIME]=@OPERATETIME,[LOGID]=@LOGID,[OPERATEBUTTON]=@OPERATEBUTTON,[REMARK]=@REMARK,[MENUID]=@MENUID wh",connection);
		update.Parameters.Add("@OPERATETIME",SqlDbType.DateTime);
		update.Parameters["@OPERATETIME"].SourceColumn="OPERATETIME";
		update.Parameters.Add("@LOGID",SqlDbType.Int);
		update.Parameters["@LOGID"].SourceColumn="LOGID";
		update.Parameters.Add("@OPERATEBUTTON",SqlDbType.VarChar);
		update.Parameters["@OPERATEBUTTON"].SourceColumn="OPERATEBUTTON";
		update.Parameters.Add("@REMARK",SqlDbType.VarChar);
		update.Parameters["@REMARK"].SourceColumn="REMARK";
		update.Parameters.Add("@MENUID",SqlDbType.Int);
		update.Parameters["@MENUID"].SourceColumn="MENUID";
		return update;
	}
	/// <summary>
	/// 获取一个更新数据表的命令对象。这个方法将不更新主键字段，其它字段全部更新。
	/// </summary>
	/// <returns>返回一个更新命令对象(SqlCommand)</returns>
	public SqlCommand GetUpdateCommand(){
		SqlCommand update = new SqlCommand("update [T_LOG_DETAILS] set [OPERATETIME]=@OPERATETIME,[LOGID]=@LOGID,[OPERATEBUTTON]=@OPERATEBUTTON,[REMARK]=@REMARK,[MENUID]=@MENUID wh",connection);
		update.Parameters.Add("@OPERATETIME",SqlDbType.DateTime);
		update.Parameters["@OPERATETIME"].SourceColumn="OPERATETIME";
		update.Parameters.Add("@LOGID",SqlDbType.Int);
		update.Parameters["@LOGID"].SourceColumn="LOGID";
		update.Parameters.Add("@OPERATEBUTTON",SqlDbType.VarChar);
		update.Parameters["@OPERATEBUTTON"].SourceColumn="OPERATEBUTTON";
		update.Parameters.Add("@REMARK",SqlDbType.VarChar);
		update.Parameters["@REMARK"].SourceColumn="REMARK";
		update.Parameters.Add("@MENUID",SqlDbType.Int);
		update.Parameters["@MENUID"].SourceColumn="MENUID";
		return update;
	}
	/// <summary>
	/// 获取一个根据主键删除数据表的命令对象。
	/// </summary>
	/// <returns>返回一个删除命令对象(SqlCommand)</returns>
	public SqlCommand GetDeleteCommand(){
		SqlCommand delete = new SqlCommand("delete from [T_LOG_DETAILS] wh",connection);
		return delete;
	}
	/// <summary>
	/// 获取一个查询一个表的所有数据的命令对象。
	/// </summary>
	/// <returns>返回一个查询命令对象(SqlCommand)</returns>
	public SqlCommand GetSelectCommand(){
		SqlCommand select = new SqlCommand ("select * from [T_LOG_DETAILS]",connection);
		return select;
	}
	/// <summary>
	/// 获取一个查询命令对象，将根据主键进行查询，只返回一条数据。
	/// </summary>
	/// <returns>返回一个查询命令对象(SqlCommand)</returns>
	public SqlCommand GetSelectCommandPrimary(){
		SqlCommand select = new SqlCommand ("select * from [T_LOG_DETAILS] wh",connection);
		return select;
	}
	/// <summary>
	/// 更新一个本地表(DataTable),包括增,删,改.修改时包括修改主键.
	/// </summary>
	/// <param name="dt">本地表.将提交给数据库服务器.</param>
	/// <returns>如果成功,返回true;否则返回false.</returns>
	public bool Update_P(DataTable dt){
		SqlDataAdapter sda = new SqlDataAdapter();
		sda.InsertCommand=GetInsertCommand();
		sda.UpdateCommand=GetUpdateCommand_P();
		sda.DeleteCommand=GetDeleteCommand();
		connection.Open();
		SqlTransaction tran =connection.BeginTransaction();
		sda.InsertCommand.Transaction = tran;
		sda.UpdateCommand.Transaction = tran;
		sda.DeleteCommand.Transaction = tran;
		try
		{
			sda.Update(dt);
			tran.Commit();
			return true;
		}
		catch{
			tran.Rollback();
			return false;
		}
		finally{ connection.Close(); }
	}
	/// <summary>
	/// 更新一个本地表(DataTable),包括增,删,改.修改时不包括修改主键.
	/// </summary>
	/// <param name="dt">本地表.将提交给数据库服务器.</param>
	/// <returns>如果成功,返回true;否则返回false.</returns>
	public bool Update(DataTable dt){
		SqlDataAdapter sda = new SqlDataAdapter();
		sda.InsertCommand=GetInsertCommand();
		sda.UpdateCommand=GetUpdateCommand();
		sda.DeleteCommand=GetDeleteCommand();
		connection.Open();
		SqlTransaction tran =connection.BeginTransaction();
		sda.InsertCommand.Transaction = tran;
		sda.UpdateCommand.Transaction = tran;
		sda.DeleteCommand.Transaction = tran;
		try
		{
			sda.Update(dt);
			tran.Commit();
			return true;
		}
		catch{
			tran.Rollback();
			return false;
		}
		finally{ connection.Close(); }
	}
	/// <summary>
	/// 查询所有的数据。
	/// </summary>
	/// <returns>查询的结果。一个本地表（DataTable）</returns>
	public DataTable Select(){
		SqlDataAdapter sda = new SqlDataAdapter("select * from [T_LOG_DETAILS]",connection);
		DataTable dt = new DataTable();
		sda.Fill(dt);
		return dt;
	}
	/// <summary>
	/// 查询一个实体，要求查询的实体主键已经被初始化，并被作为条件.查询后，其所有字段将被赋值。
	/// </summary>
	/// <param name="data">实体对象.主键应该已经被初始化。</param>
	/// <returns>如果成功,返回true;否则返回false.</returns>
	public bool Select(T_LOG_DETAILSData data){
		try{
			SqlCommand command = GetSelectCommandPrimary();
			connection.Open();
			SqlDataReader reader = command.ExecuteReader();
			if(reader.Read())
			{
				if (!reader.IsDBNull(reader.GetOrdinal("OPERATETIME")))
					data.OPERATETIME=DateTime.Parse(reader.GetValue(reader.GetOrdinal("OPERATETIME")).ToString());
				if (!reader.IsDBNull(reader.GetOrdinal("LOGID")))
					data.LOGID=Int32.Parse(reader.GetValue(reader.GetOrdinal("LOGID")).ToString());
				if (!reader.IsDBNull(reader.GetOrdinal("OPERATEBUTTON")))
					data.OPERATEBUTTON=reader.GetValue(reader.GetOrdinal("OPERATEBUTTON")).ToString();
				if (!reader.IsDBNull(reader.GetOrdinal("REMARK")))
					data.REMARK=reader.GetValue(reader.GetOrdinal("REMARK")).ToString();
				if (!reader.IsDBNull(reader.GetOrdinal("MENUID")))
					data.MENUID=Int32.Parse(reader.GetValue(reader.GetOrdinal("MENUID")).ToString());
				return true;
			}
			return false;
		}
		catch
		{
			return false;
		}
		finally
		{connection.Close();}
	}
	/// <summary>
	/// 根据条件查询一个实体.查询后，其所有字段将被赋值。
	/// </summary>
	/// <param name="data">实体对象.将被初始化后作为方法结果。</param>
	/// <param name="condition">查询条件,即where以后的语句.</param>
	/// <returns>如果成功,返回true;否则返回false.</returns>
	public bool Select(T_LOG_DETAILSData data,string condition){
		if(data == null)
			data = new T_LOG_DETAILSData();
		SqlCommand command = new SqlCommand("select * from [T_LOG_DETAILS] where "+ condition ,connection);
		connection.Open();
		SqlDataReader reader = command.ExecuteReader();
		try
		{
			if(reader.Read())
			{
				if (!reader.IsDBNull(reader.GetOrdinal("OPERATETIME")))
					data.OPERATETIME=DateTime.Parse(reader.GetValue(reader.GetOrdinal("OPERATETIME")).ToString());
				if (!reader.IsDBNull(reader.GetOrdinal("LOGID")))
					data.LOGID=Int32.Parse(reader.GetValue(reader.GetOrdinal("LOGID")).ToString());
				if (!reader.IsDBNull(reader.GetOrdinal("OPERATEBUTTON")))
					data.OPERATEBUTTON=reader.GetValue(reader.GetOrdinal("OPERATEBUTTON")).ToString();
				if (!reader.IsDBNull(reader.GetOrdinal("REMARK")))
					data.REMARK=reader.GetValue(reader.GetOrdinal("REMARK")).ToString();
				if (!reader.IsDBNull(reader.GetOrdinal("MENUID")))
					data.MENUID=Int32.Parse(reader.GetValue(reader.GetOrdinal("MENUID")).ToString());
			return true;
			}
			return false;
		}
		catch
		{
			return false;
		}
			finally
		{
			connection.Close();
		}
	}
	/// <summary>
	/// 删除一个实体。这个实体的主键将被作为删除条件.其它字段将不被用. 
	/// </summary>
	/// <param name="data">实体对象.将从数据库删除。</param>
	public void Delete(T_LOG_DETAILSData data){
		SqlCommand command = GetDeleteCommand();
		connection.Open();
		try{
		command.ExecuteNonQuery();}
		catch (System.Exception ex){throw ex;}finally{
		connection.Close();
		}
	}
	/// <summary>
	/// 更新一个实体。 根据主键更新其它字段.仅不更新主键.
	/// </summary>
	/// <param name="data">实体对象.将从数据库更新。</param>
	public void Update(T_LOG_DETAILSData data){
		SqlCommand command = GetUpdateCommand();
		if(data.OPERATETIME.Year != 1)
			command.Parameters["@OPERATETIME"].Value=data.OPERATETIME;
		else
			command.Parameters["@OPERATETIME"].Value=DBNull.Value;
		command.Parameters["@LOGID"].Value=data.LOGID;
		if(data.OPERATEBUTTON != null)
			command.Parameters["@OPERATEBUTTON"].Value=data.OPERATEBUTTON;
		else
			command.Parameters["@OPERATEBUTTON"].Value=DBNull.Value;
		if(data.REMARK != null)
			command.Parameters["@REMARK"].Value=data.REMARK;
		else
			command.Parameters["@REMARK"].Value=DBNull.Value;
		command.Parameters["@MENUID"].Value=data.MENUID;
		connection.Open();
		try{
		command.ExecuteNonQuery();}
		catch (System.Exception ex){throw ex;}finally{
		connection.Close();
		}
	}
	/// <summary>
	/// 更新一个实体。 根据主键更新全部字段.包括更新主键.
	/// </summary>
	/// <param name="data">实体对象.将被更新。其它参数作为主键条件。</param>
	public void Update_P(T_LOG_DETAILSData data){
		SqlCommand command = GetUpdateCommand_P();
		if(data.OPERATETIME.Year != 1)
			command.Parameters["@OPERATETIME"].Value=data.OPERATETIME;
		else
			command.Parameters["@OPERATETIME"].Value=DBNull.Value;
		command.Parameters["@LOGID"].Value=data.LOGID;
		if(data.OPERATEBUTTON != null)
			command.Parameters["@OPERATEBUTTON"].Value=data.OPERATEBUTTON;
		else
			command.Parameters["@OPERATEBUTTON"].Value=DBNull.Value;
		if(data.REMARK != null)
			command.Parameters["@REMARK"].Value=data.REMARK;
		else
			command.Parameters["@REMARK"].Value=DBNull.Value;
		command.Parameters["@MENUID"].Value=data.MENUID;
		connection.Open();
		try{
		command.ExecuteNonQuery();}
		catch (System.Exception ex){throw ex;}finally{
		connection.Close();
		}
	}
	/// <summary>
	/// 插入一个实体到数据库.
	/// </summary>
	/// <param name="data">实体对象.将被插入到数据库，插入包括所有字段。</param>
	public void Insert(T_LOG_DETAILSData data){
		SqlCommand command = GetInsertCommand();
		if(data.OPERATETIME.Year != 1)
			command.Parameters["@OPERATETIME"].Value=data.OPERATETIME;
		else
			command.Parameters["@OPERATETIME"].Value=DBNull.Value;
		command.Parameters["@LOGID"].Value=data.LOGID;
		if(data.OPERATEBUTTON != null)
			command.Parameters["@OPERATEBUTTON"].Value=data.OPERATEBUTTON;
		else
			command.Parameters["@OPERATEBUTTON"].Value=DBNull.Value;
		if(data.REMARK != null)
			command.Parameters["@REMARK"].Value=data.REMARK;
		else
			command.Parameters["@REMARK"].Value=DBNull.Value;
		command.Parameters["@MENUID"].Value=data.MENUID;
		connection.Open();
		try{
		command.ExecuteNonQuery();}
		catch (System.Exception ex){throw ex;}finally{
		connection.Close();
		}
	}
	/// <summary>
	/// 根据条件查询数据。
	/// </summary>
	/// <param name="expression">查询表达式</param>
	/// <returns>返回一个本地表(DataTable)</returns>
	public DataTable Select(string expression){
		SqlDataAdapter sda = new SqlDataAdapter(expression,connection);
		DataTable dt = new DataTable();
		sda.Fill(dt);
		return dt;
	}
	/// <summary>
	/// 执行一条Sql语句,没有任何返回数据。
	/// </summary>
	/// <param name="expression">执行Sql表达式</param>
	public void Excute(string expression){
		SqlCommand command = new SqlCommand(expression,connection);
		connection.Open();
		try{
		command.ExecuteNonQuery();}
		catch (System.Exception ex){throw ex;}finally{
		connection.Close();
		}
	}
	/// <summary>
	/// 执行一条Sql语句,并返回第一行第一列的数据。
	/// </summary>
	/// <param name="expression">需要执行的Sql语句。</param>
	/// <returns>返回一个本地表(DataTable)</returns>
	public object SelectScalar(string expression){
		SqlCommand command = new SqlCommand(expression,connection);
		connection.Open();
		object o = command.ExecuteScalar();
		connection.Close();
		return o;
	}
	/// <summary>
	/// 获取一个表的结构。
	/// </summary>
	/// <returns>具有数据库表结构的本地空表(DataTable)</returns>
	public DataTable GetTableSchema(){
		DataTable dt = new DataTable();
		SqlDataAdapter sda = new SqlDataAdapter("select top 0 * from [T_LOG_DETAILS]",connection);
		sda.FillSchema(dt,SchemaType.Source);
		return dt;
	}
}
}
